/*
 * 	Name		: CMX7164.h
 *	Author	 	: Tigran Papazyan
 *  Created on	: Nov 27, 2010
 *  Copyright   : Copyright Antel Systems
 *  Description : CMX7164 Driver
 */

#ifndef _CONF_CMX7164_H_
#define _CONF_CMX7164_H_

#include "conf_access.h"

//_____ D E F I N I T I O N S ______________________________________________
#define FREQ_MATRIX_SIZE 		  12
#define ACTUAL_FREQ_MATRIX_SIZE    4
#define ACTUAL_CYCLE_START        ((FREQ_MATRIX_SIZE - ACTUAL_FREQ_MATRIX_SIZE) >> 1)
#define MAX_FREQ_ERROR  		  20

#define CMX7164_PDCA_PID_TX 		AVR32_PDCA_PID_SPI_TX
#define CMX7164_PDCA_PID_RX 		AVR32_PDCA_PID_SPI_RX

#define CMX7164_SPI_IRQ				AVR32_SPI_IRQ
//Number of CMX7164 components to manage.
#define CMX7164_CNT             1

#define ROW_DATA_FRAME_SIZE		60

/**************CMX7164 IMAGE LOADING ***********/

#define CMX7164_FSK_MODULATION	1
#define CMX7164_QAM_MODULATION	2
#define CMX7164_GMSK_MODULATION	3

// --------------------------------------------------------------------------------------1.0.0.2
/*DBlock 1: DB1, Load seq 1*/
#define CMX7164_IMAGE_GMSK_DB1_PTR         0x4000  /* Data block pointer*/
#define CMX7164_IMAGE_GMSK_DB1_LEN         0x5400  /* Data block length, in 16 bit words*/
#define CMX7164_IMAGE_GMSK_DB1_CHK_HI      0x0024
#define CMX7164_IMAGE_GMSK_DB1_CHK_LO      0xC156

/*DBlock 2: DB2, Load seq 2*/
#define CMX7164_IMAGE_GMSK_DB2_PTR         0x2000  /* Data block pointer*/
#define CMX7164_IMAGE_GMSK_DB2_LEN         0x10F0  /* Data block length, in 16 bit words*/
#define CMX7164_IMAGE_GMSK_DB2_CHK_HI      0xFFF2
#define CMX7164_IMAGE_GMSK_DB2_CHK_LO      0xF1F0

#define CMX7164_IMAGE_GMSK_VERSION		  0x1002
//----------------------------------------------------------------------------------------------

// --------------------------------------------------------------------------------------4.0.0.6
/*DBlock 1: DB1, Load seq 1*/
#define CMX7164_IMAGE_QAM_DB1_PTR         0x4000  /* Data block pointer*/
#define CMX7164_IMAGE_QAM_DB1_LEN         0x5400  /* Data block length, in 16 bit words*/
#define CMX7164_IMAGE_QAM_DB1_CHK_HI      0xFFDC
#define CMX7164_IMAGE_QAM_DB1_CHK_LO      0x9589

/*DBlock 2: DB2, Load seq 2*/
#define CMX7164_IMAGE_QAM_DB2_PTR         0x2000  /* Data block pointer*/
#define CMX7164_IMAGE_QAM_DB2_LEN         0x11E0  /* Data block length, in 16 bit words*/
#define CMX7164_IMAGE_QAM_DB2_CHK_HI      0xFFF2
#define CMX7164_IMAGE_QAM_DB2_CHK_LO      0x05D4

#define CMX7164_IMAGE_QAM_VERSION		  0x4006
//----------------------------------------------------------------------------------------------


// --------------------------------------------------------------------------------------2.0.0.8
/*DBlock 1: DB1, Load seq 1*/
#define CMX7164_IMAGE_FSK_DB1_PTR         0x4000  /* Data block pointer*/
#define CMX7164_IMAGE_FSK_DB1_LEN         0x5408  /* Data block length, in 16 bit words*/
#define CMX7164_IMAGE_FSK_DB1_CHK_HI      0x0047
#define CMX7164_IMAGE_FSK_DB1_CHK_LO      0x6C41

/*DBlock 2: DB2, Load seq 2*/
#define CMX7164_IMAGE_FSK_DB2_PTR         0x2000  /* Data block pointer*/
#define CMX7164_IMAGE_FSK_DB2_LEN         0x1018  /* Data block length, in 16 bit words*/
#define CMX7164_IMAGE_FSK_DB2_CHK_HI      0xFFE7
#define CMX7164_IMAGE_FSK_DB2_CHK_LO      0xD220

#define CMX7164_IMAGE_FSK_VERSION		  0x2008
//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
/*DBlock 3: ACTIVATE, Load seq 3*/
#define CMX7164_IMAGE_ACTIVATE_PTR    0x4010
#define CMX7164_IMAGE_ACTIVATE_LEN    0

#define CMX7164_IMAGE_ID			  0x7164
/**************CMX7164 IMAGE LOADING ***********/

/**************** CMX7164 boud rate configuration options ***********/
#define CMX7164_BOUD_RATE_9600			0
#define CMX7164_BOUD_RATE_4800			1
#define CMX7164_BOUD_RATE_2400			2
#define CMX7164_BOUD_RATE_4000			3
#define CMX7164_BOUD_RATE_7200			4
#define CMX7164_BOUD_RATE_8000			5
#define CMX7164_BOUD_RATE_12000			6
#define CMX7164_BOUD_RATE_14400			7
#define CMX7164_BOUD_RATE_16000			8
#define CMX7164_BOUD_RATE_18000			9
#define CMX7164_BOUD_RATE_20000			10
#define CMX7164_BOUD_RATE_21250			11
#define CMX7164_BOUD_RATE_24000			12
#define CMX7164_BOUD_RATE_20480			13
#define CMX7164_BOUD_RATE_21750			14


/***********END of CMX7164 boud rate configuration options***********/

/****************** CMX7164 RAM DAC Array selects *******************/
#define CMX7163_RAM_DAC_2_0V	0
#define CMX7163_RAM_DAC_2_1V	1
#define CMX7163_RAM_DAC_2_2V	2
#define CMX7163_RAM_DAC_2_3V	3
#define CMX7163_RAM_DAC_2_4V	4
#define CMX7163_RAM_DAC_2_5V	5
#define CMX7163_RAM_DAC_2_6V	6
#define CMX7163_RAM_DAC_2_7V	7
#define CMX7163_RAM_DAC_3_1V	8
#define CMX7163_RAM_DAC_3_5V	9
#define CMX7163_RAM_DAC_temp	10
/********************************************************************/

/*************Status Register Bits**************/
#define Aux_ADC0_Bit				0
#define Aux_ADC1_Bit				1
#define Aux_ADC2_Bit				2
#define Aux_ADC3_Bit				3
#define Ramp_Done_Bit				4
#define SPI_Thru_Bit				5
#define Cmd_FIFO_Bit				6
#define Rx_FIFO_Bit					7
#define Cmd_Done_Bit				8
#define Rx_FS_Det_Tx_Last_Tail_Bit	9
#define Tx_Done_Bit					10
#define Rx_OV_Tx_Empty_Bit			11
#define CS_Abort_Bit				12
#define CS_Tx_Bit					13
#define Reg_Done_Bit				14
#define IRQ_Bit						15
/*************Status Register Bits**************/

/************** Modulation type ****************/
#define CMX7164_MODULATION_TYPE_4QAM	4
#define CMX7164_MODULATION_TYPE_16QAM	16
#define CMX7164_MODULATION_TYPE_64QAM	64
#define CMX7164_MODULATION_TYPE_2FSK	2
#define CMX7164_MODULATION_TYPE_4FSK	4

/************** Modulation type ****************/

/********Block Types for QAM formated data******/
#define CMX7164_BLOCK_TYPE_HEADER		5
#define CMX7164_BLOCK_TYPE_INTERMEDIATE	6
#define CMX7164_BLOCK_TYPE_LAST			7
/********Block Types for QAM formated data******/

/*******Block Types for GMSK formated data******/
#define CMX7164_BLOCK_TYPE_FRAME_HEAD				5
#define CMX7164_BLOCK_TYPE_DATA_BLOCK_WITH_CRC		6
#define CMX7164_BLOCK_TYPE_DATA_BLOCK_WITHOUT_CRC 	7
#define CMX7164_BLOCK_TYPE_PREAMBLE_SYMBOLS		 	12
#define CMX7164_BLOCK_TYPE_FS_SYMBOLS				13
#define CMX7164_BLOCK_TYPE_END_TX					15
/*******Block Types for GMSK formated data******/

/********************* RRC *********************/
#define INPUT_RRC_FILTER_25K			0x00
#define INPUT_RRC_FILTER_50K			0x01
#define INPUT_RRC_FILTER_100K			0x02
#define INPUT_RRC_FILTER_200K			0x03

#define OUTPUT_RRC_FILTER_12_5K			0x00
#define OUTPUT_RRC_FILTER_25K			0x01
#define OUTPUT_RRC_FILTER_50K			0x02
#define OUTPUT_RRC_FILTER_100K			0x03
/********************* RRC *********************/

/******************* RAM DAC *******************/
#define CMX7164_RAM_DAC_RAMP_UP				1
#define CMX7164_RAM_DAC_RAMP_DOWN			2
#define CMX7164_RAM_DAC_NORMAL_OPERATION	3
/******************* RAM DAC *******************/

/***************** Packet Type *****************/
#define TRIM_PACKET							1
#define PACIFICREST							2
#define SATEL								3
/***************** Packet Type *****************/


/************** CM7164 Registers ***************/
typedef struct
{
	unsigned char reserved ;
	unsigned char address ;
	unsigned short data ;
}CMX_WORD;

typedef struct
{
	unsigned char address ;
	unsigned char data ;
}CMX_BYTE;

unsigned char   C_BUS_Reset;

struct modem_command_fifo_data_byte_struct
{
	unsigned char	address;
	unsigned char data;
} modem_command_fifo_data_byte;

struct modem_command_fifo_data_word_struct
{
	unsigned char reserved;
	unsigned char address;
	unsigned short data;
}modem_command_fifo_data_word;

struct modem_command_control_byte_struct
{
	unsigned char	address;
	unsigned char data;
}modem_command_control_byte;

struct modem_command_fifo_level_struct
{
	unsigned char	address;
	unsigned char data;
}modem_command_fifo_level;

struct rx_fifo_data_byte_struct
{
	unsigned char	address;
	unsigned char	data;
}rx_fifo_data_byte;

struct rx_fifo_data_word_struct
{
	unsigned char	reserved;
	unsigned char	address;
	unsigned short	data;
}rx_fifo_data_word;

struct rx_fifo_control_byte_struct
{
	unsigned char	address;
	unsigned char	data;
}rx_fifo_control_byte;

struct rx_fifo_level_struct
{
	unsigned char	address;
	unsigned char	data;
}rx_fifo_level;

struct fifo_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	R_Clr :1 ;
			unsigned short	Rx_FIFO_Interrupt_Level_Threshold :7 ;
			unsigned short	C_Clr :1 ;
			unsigned short	Command_FIFO_Interrupt_Level_Threshold :7 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}fifo_control;

struct aux_adc_0_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	:1 ;			//0
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	Sam :1 ;
			unsigned short	Source :3 ;
			unsigned short	:4 ;
			unsigned short	Averaging_Counter :4 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_0_control;

struct aux_adc_1_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	:1 ;			//0
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	Sam :1 ;
			unsigned short	Source :3 ;
			unsigned short	:4 ;
			unsigned short	Averaging_Counter :4 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_1_control;

struct aux_adc_2_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	:1 ;			//0
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	Sam :1 ;
			unsigned short	Source :3 ;
			unsigned short	:4 ;
			unsigned short	Averaging_Counter :4 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_2_control;

struct aux_adc_3_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	:1 ;			//0
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	Sam :1 ;
			unsigned short	Source :3 ;
			unsigned short	:4 ;
			unsigned short	Averaging_Counter :4 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_3_control;

struct aux_adc_0_thresholds_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	High_Threshold :8 ;
			unsigned short	Low_Threshold :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_0_thresholds;

struct aux_adc_1_thresholds_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	High_Threshold :8 ;
			unsigned short	Low_Threshold :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_1_thresholds;

struct aux_adc_2_thresholds_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	High_Threshold :8 ;
			unsigned short	Low_Threshold :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_2_thresholds;

struct aux_adc_3_thresholds_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	High_Threshold :8 ;
			unsigned short	Low_Threshold :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_3_thresholds;

struct aux_dac_0_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	RAM :1 ;
			unsigned short	Down :1 ;
			unsigned short	Up :1 ;
			unsigned short	reserved :2 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_dac_0_control;

struct aux_dac_1_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	RAM :1 ;
			unsigned short	Down :1 ;
			unsigned short	Up :1 ;
			unsigned short	reserved :2 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_dac_1_control;

struct aux_dac_2_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	RAM :1 ;
			unsigned short	Down :1 ;
			unsigned short	Up :1 ;
			unsigned short	reserved :2 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_dac_2_control;

struct aux_dac_3_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Ena :1 ;
			unsigned short	RAM :1 ;
			unsigned short	Down :1 ;
			unsigned short	Up :1 ;
			unsigned short	reserved :2 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_dac_3_control;

struct aux_adc_0_read_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	reserved :4 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_0_read;

struct aux_adc_1_read_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	reserved :4 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_1_read;

struct aux_adc_2_read_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	reserved :4 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_2_read;

struct aux_adc_3_read_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short	Hi :1 ;
			unsigned short	Lo :1 ;
			unsigned short	reserved :4 ;
			unsigned short	Sample_Value :10 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}aux_adc_3_read;

struct I_output_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short select:4 ;
				unsigned short Fine_Gain :12;
			}Select_0000;
			struct
			{
				unsigned short select:4 ;
				unsigned short DC_Offset_upper :12;
			}Select_0001;
			struct
			{
				unsigned short select:4 ;
				unsigned short :8 ;
				unsigned short DC_Offset_lower :4 ;
			}Select_0010;
			struct
			{
				unsigned short select:4 ;
				unsigned short reserved :12 ;
			}Select_0011_to_1111;
		}Bit_val;
		unsigned short Word_val ;
	};
}I_output_control;

struct Q_output_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short	select:4 ;
				unsigned short Fine_Gain :12;
			}Select_0000;
			struct
			{
				unsigned short	select:4 ;
				unsigned short DC_Offset_upper :12;
			}Select_0001;
			struct
			{
				unsigned short	select:4 ;
				unsigned short :8 ;
				unsigned short DC_Offset_lower :4 ;
			}Select_0010;
			struct
			{
				unsigned short	select:4 ;
				unsigned short reserved :12 ;
			}Select_0011_to_1111;
		}Bit_val;
		unsigned short Word_val ;
	};
}Q_output_control;

struct I_input_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short	select:4 ;
				unsigned short Fine_Gain :12;
			}Select_0000;
			struct
			{
				unsigned short	select:4 ;
				unsigned short DC_Offset :12;
			}Select_0001;
			struct
			{
				unsigned short	select:4 ;
				unsigned short reserved :12 ;
			}Select_0010_to_1111;
		}Bit_val;
		unsigned short Word_val ;
	};
}I_input_control;

struct Q_input_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short select:4 ;
				unsigned short Fine_Gain :12;
			}Select_0000;
			struct
			{
				unsigned short select:4 ;
				unsigned short DC_Offset :12;
			}Select_0001;
			struct
			{
				unsigned short select:4 ;
				unsigned short reserved :12 ;
			}Select_0010_to_1111;
		}Bit_val;
		unsigned short Word_val ;
	};
}Q_input_control;

struct signal_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short select:4 ;
				unsigned short Manual_nAuto :1 ;
				unsigned short :3 ;
				unsigned short I_OUTPUT :2 ;
				unsigned short Q_OUTPUT :2 ;
				unsigned short I_INPUT  :2 ;
				unsigned short Q_INPUT  :2 ;
			}Select_0001;
			struct
			{
				unsigned short select:4 ;
				unsigned short :4 ;
				unsigned short GPIO_Trigger_Select :4 ;
				unsigned short I_Q_OUTPUT_Function :4 ;
			}Select_0011;
			struct
			{
				unsigned short select:4 ;
				unsigned short I_Q_INPUT_DC_Correction_Loop_Gain :12;
			}Select_0100;
			struct
			{
				unsigned short select:4 ;
				unsigned short AGC_Low_Threshold :12 ;
			}Select_0101;
			struct
			{
				unsigned short select:4 ;
				unsigned short AGC_Low_Timer :12 ;
			}Select_0110;
			struct
			{
				unsigned short select:4 ;
				unsigned short AGC_Settling_Timer :12 ;
			}Select_1000;
			struct
			{
				unsigned short select:4 ;
				unsigned short AGC_High_Threshold :12 ;
			}Select_1001;
		}Bit_val;
		unsigned short Word_val ;
	};
}signal_control;

struct agc_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short reserved :5 ;
			unsigned short IQDC_Latch :1 ;
			unsigned short IQDC_Table :1 ;
			unsigned short AGC_Start_Max :1 ;
			unsigned short AGC_Mode :4 ;
			unsigned short Manual_Gain :4 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}agc_control;

struct rx_tracking_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short reserved :8 ;
			unsigned short IQDC_Mode :4 ;
			unsigned short Timing_Mode :4 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}rx_tracking;

struct reg_done_select_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short reserved :12 ;
			unsigned short Mode :1 ;
			unsigned short GPIO :1 ;
			unsigned short FIFO_Con :1 ;
			unsigned short Prog :1 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}reg_done_select;

struct internal_buffer_fill_levels_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short Command_Buffer_ill_Level :8 ;
			unsigned short Rx_Data_Buffer_Fill_Level :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}internal_buffer_fill_levels;

struct I_input_offset_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	unsigned short I_Q_DC_Offset_Applied_to_the_I_Q_INPUTs ;
}I_input_offset;

struct Q_input_offset_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	unsigned short I_Q_DC_Offset_Applied_to_the_I_Q_INPUTs ;
}Q_input_offset;

struct agc_gain_rssi_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short AGC_Gain :4 ;
			unsigned short RSSI_Readback :12 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}agc_gain_rssi;

struct rx_error_magnitude_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	unsigned short Rx_Error_Magnitude ;
}rx_error_magnitude ;

struct frequency_error_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	unsigned short Frequency_Error ;
}frequency_error ;

struct spi_talkthrough_address_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short CS :2 ;
				unsigned short IRQ_Mode :1 ;
				unsigned short RnW :1 ;
				unsigned short reserved :2 ;
				unsigned short DataBytes :2 ;
				unsigned short Address :8 ;
			}C_BUS_Mode;
			struct
			{
				unsigned short CS :2 ;
				unsigned short IRQ_Mode :1 ;
				unsigned short Data_Bits :5 ;
				unsigned short SPI_Write_Data	 :8 ;
			}SPI_Mode ;
		}Bit_val;
		unsigned short Word_val ;
	};
}spi_talkthrough_address;

struct spi_talkthrough_write_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			union
			{
				unsigned short C_BUS_Write_Data_Word ;
				struct
				{
					unsigned short reserved :8 ;
					unsigned short C_BUS_Write_Data_Byte :8 ;
				};
			}C_BUS_Mode;
			struct
			{
				unsigned short SPI_Write_Data ;
			}SPI_Mode ;
		}Bit_val;
		unsigned short Word_val ;
	};
}spi_talkthrough_write;

struct gpio_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short GPIO_Input_Output_Select :8 ;
			unsigned short GPIO_Output_Value :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}gpio_control ;

struct spi_talkthrough_read_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		unsigned short C_BUS_Read_Data_word ;
		struct
		{
			unsigned short :8 ;
			unsigned short C_BUS_Read_Data_byte :8 ;
		}C_BUS_Read_Data_byte;
		unsigned short SPI_Read_Data ;
	};
}spi_talkthrough_read ;

struct gpio_read_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		struct
		{
			unsigned short :8 ;
			unsigned short GPIO_Input_Values :8 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}gpio_read ;

struct programming_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	unsigned short Program_Block_Data;
}programming ;

struct modem_mode_and_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	union
	{
		union
		{
			struct
			{
				unsigned short :4 ;
				unsigned short Modem_Control_Rx :4 ;
				unsigned short Modem_Control_Tx_CS :4 ;
				unsigned short :2 ;
				unsigned short Rx_Tx_CS :2 ;
			}Rx_Tx_CS_11;
			struct
			{
				unsigned short Program_Sub_block_s :8 ;
				unsigned short Program_Block_b :4 ;
				unsigned short :2 ;
				unsigned short Idle :2 ;
			}Idle_00;
		}Bit_val;
		unsigned short Word_val ;
	};
}modem_mode_and_control ;

struct irq_enable_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0x6C
	union
	{
		struct
		{
			unsigned short IRQ :1 ;
			unsigned short Reg_Done :1 ;
			unsigned short CS_Tx :1 ;
			unsigned short CS_Abort :1 ;
			unsigned short Rx_OV_Tx_Empty :1 ;
			unsigned short Tx_Done :1 ;
			unsigned short Rx_FS_Det_Tx_Last_Tail :1 ;
			unsigned short Cmd_Done :1 ;
			unsigned short Rx_FIFO :1 ;
			unsigned short Cmd_FIFO :1 ;
			unsigned short SPI_Thru :1 ;
			unsigned short Ramp_Done :1 ;
			unsigned short Aux_ADC3 :1 ;
			unsigned short Aux_ADC2 :1 ;
			unsigned short Aux_ADC1 :1 ;
			unsigned short Aux_ADC0 :1 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}irq_enable ;

struct irq_status_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0x7E
	union
	{
		struct
		{
			unsigned short IRQ :1 ;
			unsigned short Reg_Done :1 ;
			unsigned short CS_Tx :1 ;
			unsigned short CS_Abort :1 ;
			unsigned short Rx_OV_Tx_Empty :1 ;
			unsigned short Tx_Done :1 ;
			unsigned short Rx_FS_Det_Tx_Last_Tail :1 ;
			unsigned short Cmd_Done :1 ;
			unsigned short Rx_FIFO :1 ;
			unsigned short Cmd_FIFO :1 ;
			unsigned short SPI_Thru :1 ;
			unsigned short Ramp_Done :1 ;
			unsigned short Aux_ADC3 :1 ;
			unsigned short Aux_ADC2 :1 ;
			unsigned short Aux_ADC1 :1 ;
			unsigned short Aux_ADC0 :1 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}irq_status ;

struct modem_control_readback_struct
{
	unsigned char  	reserved ;
	unsigned char	address;
	unsigned short Readback_Value_for_Modem_Mode_and_Control_Register;
}modem_control_readback ;

struct I_Q_input_config_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB0
	union
	{
		struct
		{
			unsigned short I_IN_Filt_BW :2 ;
			unsigned short Q_IN_Filt_BW :2 ;
			unsigned short I_IN_Pwr :1 ;
			unsigned short Q_IN_Pwr :1 ;
			unsigned short :4 ;
			unsigned short Q_IN_Pwr_Drv :1 ;
			unsigned short Q_IN_Mute :1 ;
			unsigned short :2 ;
			unsigned short I_IN_Pwr_Drv :1 ;
			unsigned short I_IN_Mute :1 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}I_Q_input_config ;

struct I_input_coarse_gain_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB1
	union
	{
		struct
		{
			unsigned short :13 ;
			unsigned short IP1_2_Gain :3 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}I_input_coarse_gain ;

struct Q_input_coarse_gain_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB2
	union
	{
		struct
		{
			unsigned short :13 ;
			unsigned short IP1_2_Gain :3 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}Q_input_coarse_gain ;

struct I_Q_output_config_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB3
	union
	{
		struct
		{
			unsigned short IOUT_Filt_BW:2 ;
			unsigned short QOUT_Filt_BW:2 ;
			unsigned short IOUT_Pwr :1 ;
			unsigned short QOUT_Pwr :1 ;
			unsigned short :4 ;
			unsigned short QOUT_DrvPwr :1 ;
			unsigned short QOUT_Mute :1 ;
			unsigned short :2 ;
			unsigned short IOUT_DrvPwr :1 ;
			unsigned short IOUT_Mute :1 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}I_Q_output_config ;

struct I_output_coarse_gain_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB4
	union
	{
		struct
		{
			unsigned short Plus_6_db :1 ;
			unsigned short :8 ;
			unsigned short Attenuation :7 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}I_output_coarse_gain ;

struct Q_output_coarse_gain_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB5
	union
	{
		struct
		{
			unsigned short Plus_6_db :1 ;
			unsigned short :8 ;
			unsigned short Attenuation :7 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}Q_output_coarse_gain ;

struct vbias_control_struct
{
	unsigned char  	reserved ;
	unsigned char	address;			//0xB7
	union
	{
		struct
		{
			unsigned short :15 ;
			unsigned short BIAS :1 ;
		}Bit_val;
		unsigned short Word_val ;
	};
}vbias_control ;

/*
struct CMX7164_Register_struct
{
//	unsigned char * 											C_BUS_Reset ;
	void*			modem_command_fifo_data_byte ;
	void*			modem_command_fifo_data_word ;
	void*			modem_command_control_byte ;
	void*			modem_command_fifo_level ;
	void*			rx_fifo_data_byte ;
	void*			rx_fifo_data_word ;
	void*			rx_fifo_control_byte ;
	void*			rx_fifo_level ;
	void*			fifo_control ;
	void*			aux_adc_0_control ;
	void*			aux_adc_1_control ;
	void*			aux_adc_2_control ;
	void*			aux_adc_3_control ;
	void*			aux_adc_0_thresholds ;
	void*			aux_adc_1_thresholds ;
	void*			aux_adc_2_thresholds ;
	void*			aux_adc_3_thresholds ;
	void*			aux_dac_0_control ;
	void*			aux_dac_1_control ;
	void*			aux_dac_2_control ;
	void*			aux_dac_3_control ;
	void*			aux_adc_0_read ;
	void*			aux_adc_1_read ;
	void*			aux_adc_2_read ;
	void*			aux_adc_3_read ;
	void*			I_output_control ;
	void*			Q_output_control ;
	void*			I_input_control ;
	void*			Q_input_control ;
	void*			signal_control ;
	void*			agc_control ;
	void*			rx_tracking ;
	void*			reg_done_select ;
	void*			internal_buffer_fill_levels ;
	void*			I_input_offset ;
	void*			Q_input_offset ;
	void*			agc_gain_rssi ;
	void*			rx_error_magnitude ;
	void*			frequency_error ;
	void*			spi_talkthrough_address ;
	void*			spi_talkthrough_write ;
	void*			gpio_control ;
	void*			spi_talkthrough_read ;
	void*			gpio_read ;
	void*			programming ;
	void*			modem_mode_and_control ;
	void*			irq_enable ;
	void*			irq_status ;
	void*			modem_control_readback ;
	void*			I_Q_input_config ;
	void*			I_input_coarse_gain ;
	void*			Q_input_coarse_gain ;
	void*			I_Q_output_config ;
	void*			I_output_coarse_gain ;
	void*			Q_output_coarse_gain ;
	void*			vbias_control ;
}CMX7164_Register;
*/
										//Address			Operation 		BIT
#define C_BUS_RESET							0x01 			//Write		 	0
#define MODEM_COMMAND_FIFO_DATA_BYTE		0x48 			//Write    		8
#define MODEM_COMMAND_FIFO_DATA_WORD		0x49 			//Write    		16
#define MODEM_COMMAND_CONTROL_BYTE			0x4A 			//Write    		8
#define MODEM_COMMAND_FIFO_LEVEL			0x4B 			//Read     		8
#define RX_FIFO_DATA_BYTE					0x4C 			//Read     		8
#define RX_FIFO_DATA_WORD					0x4D 			//Read     		16
#define RX_FIFO_CONTROL_BYTE				0x4E 			//Read     		8
#define RX_FIFO_LEVEL						0x4F 			//Read     		8
#define FIFO_CONTROL						0x50 			//Write    		16
#define AUX_ADC_0_CONTROL					0x51 			//Write		 	16
#define AUX_ADC_1_CONTROL					0x52 			//Write		 	16
#define AUX_ADC_2_CONTROL					0x53 			//Write    		16
#define AUX_ADC_3_CONTROL					0x54 			//Write    		16
#define AUX_ADC_0_THRESHOLDS				0x55 			//Write    		16
#define AUX_ADC_1_THRESHOLDS				0x56 			//Write    		16
#define AUX_ADC_2_THRESHOLDS				0x57 			//Write    		16
#define AUX_ADC_3_THRESHOLDS				0x58 			//Write    		16
#define AUX_DAC_0_CONTROL					0x59 			//Write    		16
#define AUX_DAC_1_CONTROL					0x5A 			//Write    		16
#define AUX_DAC_2_CONTROL					0x5B 			//Write    		16
#define AUX_DAC_3_CONTROL					0x5C 			//Write    		16
#define AUX_ADC_0_READ						0x71 			//Read		 	16
#define AUX_ADC_1_READ						0x72 			//Read     		16
#define AUX_ADC_2_READ						0x73 			//Read     		16
#define AUX_ADC_3_READ						0x74 			//Read     		16
#define I_OUTPUT_CONTROL					0x5D 			//Write		 	16
#define Q_OUTPUT_CONTROL					0x5E 			//Write    		16
#define I_INPUT_CONTROL						0x5F 			//Write    		16
#define Q_INPUT_CONTROL						0x60 			//Write    		16
#define SIGNAL_CONTROL						0x61 			//Write    		16
#define AGC_CONTROL							0x65 			//Write    		16
#define RX_TRACKING							0x66 			//Write    		16
#define REG_DONE_SELECT						0x69 			//Write    		16
#define INTERNAL_BUFFER_FILL_LEVELS			0x70 			//Read     		16
#define I_INPUT_OFFSET						0x75 			//Read     		16
#define Q_INPUT_OFFSET						0x76 			//Read     		16
#define AGC_GAIN_RSSI						0x77 			//Read     		16
#define RX_ERROR_MAGNITUDE					0x7A 			//Read     		16
#define FREQUENCY_ERROR						0x7B 			//Read     		16
#define SPI_TALKTHROUGH_ADDRESS				0x62 			//Write    		16
#define SPI_TALKTHROUGH_WRITE			    0x63 			//Write    		16
#define GPIO_CONTROL						0x64 			//Write    		16
#define SPI_TALKTHROUGH_READ				0x78 			//Read     		16
#define GPIO_READ							0x79 			//Read     		16
#define PROGRAMMING							0x6A 			//Write    		16
#define MODEM_MODE_AND_CONTROL				0x6B 			//Write    		16
#define IRQ_ENABLE							0x6C 			//Write    		16
#define IRQ_STATUS							0x7E 			//Read     		16
#define MODEM_CONTROL_READBACK				0x7F 			//Read     		16
#define I_Q_INPUT_CONFIG					0xB0 			//Write    		16
#define I_INPUT_COARSE_GAIN					0xB1 			//Write    		16
#define Q_INPUT_COARSE_GAIN					0xB2 			//Write    		16
#define I_Q_OUTPUT_CONFIG					0xB3 			//Write    		16
#define I_OUTPUT_COARSE_GAIN				0xB4 			//Write    		16
#define Q_OUTPUT_COARSE_GAIN				0xB5 			//Write    		16
#define VBIAS_control						0xB7 			//Write    		16


#define RF_SWITCH_TX	1
#define RF_SWITCH_RX	0


unsigned char CMX7164_Reinit;
/*function prototypes */

int		CMX7164_Initialize(void);
void	CMX7164_Write_WORD(CMX_WORD* );
void	CMX7164_Write_BYTE(CMX_BYTE* );
void	CMX7164_Read_BYTE(CMX_BYTE* );
void	CMX7164_Read_WORD(CMX_WORD* );
int		CMX7164_Check_Status(unsigned short Status_Reg_Bit_Num);
int		CMX7164_Image_loading(unsigned short modulation_type);
void	CMX7164_IO_Init(void);
char	CMX7164_SPI_Write(unsigned char address, unsigned char* in, unsigned int len);
char	CMX7164_SPI_Read(unsigned char address, unsigned char* out, unsigned int len);

int		CMX7164_Boud_Rate_Operation(unsigned char Boud_rate);
int		CMX7164_Set_Cannel_Spec(unsigned short Spacing);
int		CMX7164_Select_Preamble_and_Length(void);
int		CMX7164_RAMDAC_Config(unsigned char mode);
double	CMX7163_Set_RAM_DAC_Array(double voltage);
int		CMX7164_GPIO_config(void);

void	CMX7164_RX_Enable(unsigned int enable);
void	CMX7164_Tx_Enable(unsigned char Enable);
void	RF_Switch_Ctrl(unsigned char TX_RX);
void 	CMX7164_Rx_SHDN(unsigned char Enable);

void	CMX7164_IO_Filter_Bendwidth_select(unsigned char input_filter ,unsigned char output_filter);
void	CMX7164_AGC_Config(void);
int		CMX7164_SSOUT_Port_Config(void);

int		CMX7164_SYSCLK_Config(void);
void 	CMX_delay(int ms);

void	CMX7164_IRQ_Init(void);

void	CMX7164_Set_Block_Type(/*unsigned char Block_Type,*/ unsigned char Block_size_index,unsigned char modulation);
int		CMX7164_Set_modulation_type(unsigned int modulation_type);
int 	CMX7164_Set_modulation_type_command(unsigned int modulation_type);
int		CMX7164_set_Preamble_len(unsigned char len , unsigned char value);
int		CMX7164_set_FS_len(unsigned int len , unsigned short FS_data_0, unsigned short FS_data_1, unsigned short FS_data_2);
int		CMX7164_set_Teil_len(unsigned char len , unsigned short value);
void	CMX7164_Frequency_fine_correction(void);

int		CMX7164_Tx_DC_Calibration_Setup(void);
int		CMX7164_TX_DC_Offset_Calibration(void);
void	CMX7164_TX_DC_Offset_Correction(unsigned short output_power_dbm_05);

void	CMX7164_Invert_I_Q(void) ;
void	CMX7164_SET_I_Q_Output_Coarse_Gain(void);

void	CMX7164_IDLE(void);

int		CMX7164_Transmit_Servise_Block(unsigned char *data);
int		CMX7164_Transmit_Row_Data(unsigned char* data ,int data_size);
int		CMX7164_Transmit_Intermediate_bloks(unsigned char* data , unsigned int size,unsigned char block_size_index);

int		CMX7164_Preamble_Tx(unsigned char Rump_up);
int		CMX7164_Deviation_Tx(unsigned char Rump_up);

int		CMX7164_Receive_MAC_Header(unsigned char* data , unsigned char block_size_index);
int		CMX7164_Receive_MAC_Payload(unsigned int size);

int		CMX7164_Receive_Intermediate_bloks(unsigned char* data , unsigned int size);
int		CMX7164_Receive_Row_Data(unsigned char* data_buf,unsigned int byte_count);
int		CMX7164_Receive_PCC_frame(void);

void	CMX7164_Disable_Interrupt(void) ;
void	CMX7164_Set_Interrupt_Source(unsigned short Source_bit);

void	CMX7164_print_info(void);
void	CMX7164_Store_statistics(void);
void	CMX7164_Print_RSSI_statistics(void);
double	CMX7164_GET_EVM(void);

void	CMX7164_Reinitialize(void);

void 	CMX7164_Set_FEC(void);

void	CMX7164_FSK_GMSK_set_deviation(unsigned int deviation_hz , unsigned int symol_rate);
/**************************/
extern const unsigned int BlockSize_QAM[];
extern const unsigned int BlockSize_4FSK[];
extern const unsigned int BlockSize_GMSK[];
extern unsigned char CMX_nInitialized ;
unsigned short CMX7164_Modulation_Type ;

short K_COEFFICIENT_OF_RSSI;
short B_COEFFICIENT_OF_RSSI;
short RSSI_Statistic;

unsigned char Rtos_started;

#endif  // _CONF_CMX7164_H_


